package com.stu.manager.studentscoremanagersys.res;

import com.stu.manager.studentscoremanagersys.enums.CodeEnum;
import lombok.Data;

import java.util.Date;

@Data
public class Result<T> {
    private Integer code = CodeEnum.SUCCESS.getCode();
    private String message = CodeEnum.SUCCESS.getMessage();
    private T data;
    private Long total;
    private Boolean isScuess = Boolean.TRUE;

    public Result() {}

    /**
     * 请求成功构建
     */
    public static <T> Result<T> buildEmptyResult(){
        return new Result<T>();
    }

    public static <T> Result<T> buildScuess(T t){
        Result<T> result = buildEmptyResult();
        result.setData(t);
        return result;
    }

    public static <T> Result<T> buildSuccess(T t,Long total){
        Result<T> result = buildScuess(t);
        result.setTotal(total);
        return result;
    }

    /**
     * 失败返回
     */

    public static <T> Result<T> buildFailure(String message){
        Result<T> result = new Result<>();
        result.setCode(null);
        result.setMessage(message);
        result.setIsScuess(false);
        return result;
    }

    public static <T> Result<T> buildFailure(Integer code,String message){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setIsScuess(false);
        return result;
    }

    public static <T> Result<T> buildFailure(CodeEnum codeEnum){
        Result<T> result = new Result<>();
        result.setCode(codeEnum.getCode());
        result.setMessage(codeEnum.getMessage());
        result.setIsScuess(false);
        return result;
    }

    /**
     * 判断成功失败
     */

    public   boolean isSuccess(){
        return isScuess;
    }

    public   boolean isFailure(){
        return !isScuess;
    }

}
